package com.apestech.framework.sql.lib;

import com.apestech.framework.dictionary.DataDictionary;
import com.apestech.framework.json.JSONArray;
import com.apestech.framework.json.JSONObject;
import com.apestech.framework.sql.MultiDataBase;

import java.util.ArrayList;

/**
 * 针对单表创建、字段结构修改、SQL解析
 *
 * @author hup
 * @create 2018-03-21-9:05
 */
public class DDLSql {

    private String tableName;
    private ArrayList<String> colList=new ArrayList<String>();
    private ArrayList<String> otherList=new ArrayList<String>();
    private ArrayList<String> sqlList=new ArrayList<String>();
    private String keyField;
    private String keyStr;
    private String equalCol;
    private String variableCol;
    private String col;
    private String head;
    private String tail;
    private MultiDataBase db;

    public DDLSql() throws Exception {}

    public DDLSql(String tableName) throws Exception {
        setTableName(tableName);
    }

    public DDLSql(String tableName, String dbType) throws Exception {
        setTableName(tableName);
        setDbType(dbType);
    }

    public void setTableName(String tableName){
        tableName=tableName.toUpperCase();
        this.tableName=tableName;
    }

    public void setDbType(String dbType){
        this.db=new MultiDataBase(dbType);
    }

    private void clear(){
        this.otherList.clear();
        this.colList.clear();
        this.sqlList.clear();
        this.col="";
        this.equalCol="";
        this.head="";
        this.tail="";
        this.keyField="";
        this.keyStr="";
        this.variableCol="";
    }

    public String dropTable() throws Exception {
        return db.getDropTable(this.tableName);
    }

    public void makeCreateTable(JSONArray tabList) throws Exception {
        makeCreateTable(tabList, null);
    }

    public void makeCreateTable(JSONArray tabList, String tableMemo) throws Exception {
        clear();
        if(tabList==null) return;
        int len=tabList.size();
        if(len<0) return;
        head="CREATE TABLE "+tableName+" (\r\n";
        tail="\r\n)";

        for(int i=0; i<len; i++){
            JSONObject map=(JSONObject)tabList.get(i);
            String id=map.getString("id");
            String type=map.getString("type");
            boolean notnull=map.getBoolean("notnull");
            String defaultValue=map.getString("default");
            String name=map.getString("name");
            boolean iskey=map.getBoolean("key");
            String s=this.db.getCreateCol(id, type, notnull, defaultValue);
            if(s!=null){
                this.colList.add(s);
            }
            if(name!=null){
                s=this.db.getComment(name, tableName, id);
                if(s!=null){
                    this.otherList.add(s);
                }
            }
            if(iskey){
                if(!"".equals(this.keyField)){
                    this.keyField=this.keyField+",";
                }
                this.keyField=this.keyField+id;
            }
        }
        if(!"".equals(this.keyField)){
            this.keyStr=db.getAddPrimaryKey(tableName, this.keyField);
        }

        StringBuilder sb=new StringBuilder();
        len=this.colList.size();
        sb.append(this.head);
        for(int i=0; i<len; i++){
            if(i>0){
                sb.append(",\r\n");
            }
            sb.append("    "+this.colList.get(i));
        }
        sb.append(this.tail);
        this.sqlList.add(sb.toString());
        this.sqlList.addAll(this.otherList);
        this.sqlList.add(this.keyStr);
        if(tableMemo!=null){
            this.sqlList.add(this.db.getComment(tableMemo, tableName, null));
        }
    }

    public ArrayList<String> getSql(){
        return this.sqlList;
    }

    /*create table tb_dept(
			    Id int primary key auto_increment,#部门编号 整形 主键 自增长
			     Name varchar(18),#部门名称
			     description varchar(100)#描述
			);*/

}
